from alive_progress import alive_bar
from dataclasses import dataclass
import time
from typing import TYPE_CHECKING
from multiprocessing import Pool, Manager
from rich import print

from run_solver import _solve
from solver.solver import Solution, solve
from visualizer.solution_visualizer import visualize_sd
import visualizer.common as v
from util.load_puzzles import load_from_file
from util.SConvert import make_blank, make_sboard, make_scomponents
from container import *

if TYPE_CHECKING:
    from container import nonBlankLabel_t
    from puzzle.board import Board
    from solver.SComponent import SComponent
    from multiprocessing.synchronize import Lock

nsl = []

if True:

    tot_ns = 0
    min_ns = 9999999
    max_ns = -1
    
    cnt1 = 0

    with open("./output/ff_c3.txt", "r") as fhand:
        def _readline():
            return fhand.readline().strip()
        
        _readline()
        
        while line := _readline():
            m, d, w, ns, fi, ti = line.replace(" ", "")[1 : -1].split("|")

            nsl.append(int(ns))
            
            ns, fi, ti = int(ns), int(fi), int(ti)
            
            tot_ns += ns
            if ns > max_ns:
                max_ns = ns
                print("Max", m, d, w)
            if ns < min_ns:
                min_ns = ns
                print("Min", m, d, w)
            
            if ns == 1:
                cnt1 += 1
                # print(m, d, w)
                
                
    print(tot_ns / 2562, min_ns, max_ns)
    print(cnt1)

if False:
    
    board, _ = load_from_file("Design 2", "./data/des2.txt", validation = False)
    _, comps = load_from_file("", "./data/des2_comp.txt")
    
    print(f"Working on board {board.name}")
    
    sc = []
    for transforms in comps:
        sc.append(make_scomponents(transforms))
    
    m, d, w = "Jan 14 Fri".split(" ")
    
    sol = _solve(board, sc, m, d, w, False)
        
    sb = make_sboard(board)
    
    make_blank(sb, board.get_td_by_label(m))
    make_blank(sb, board.get_td_by_label(d))
    make_blank(sb, board.get_td_by_label(w))
    
    visualize_sd(
        sb,
        sol[0].sd
    )
    v.show()
    
if True:
    step = 300
    cnt = [0] * (max_ns // step + 1)
    for ns in nsl:
        idx = ns // step
        cnt[idx] += 1
        
    print(cnt)
    
    import matplotlib.pyplot as plt
    
    plt.rcParams["font.family"] = "Cascadia Mono, SimHei"

    minv, maxv, avgv = min_ns, max_ns, int(tot_ns / 2562)
    
    plt.figure(figsize=(10, 6))
    
    b = [0, 150, 300, 450, 600, 750, 900, 1050, 1200, 1350, 1500, 1650, 1800, 1950, 2100, 2250, 2400, 2550, 2700, 2850]
    n, bins, patches = plt.hist(nsl, bins=b)
    
    plt.title('解数量的分布')
    plt.xlabel('解的数量')
    plt.ylabel('天数')
    
    plt.axvline(x = minv, color='g', linestyle='--', label='最小值')
    plt.axvline(x = maxv, color='b', linestyle='--', label='最大值')
    plt.axvline(x = avgv, color='r', linestyle='--', label='平均值')
    
    plt.text(minv + 75, 460, str(minv), color='g', verticalalignment = 'top', horizontalalignment = "right")
    plt.text(maxv + 100, 460, str(maxv), color='b', verticalalignment = 'top', horizontalalignment = "right")
    plt.text(avgv + 100, 460, str(avgv), color='r', verticalalignment = 'top', horizontalalignment = "right")
    
    plt.xticks(b, rotation=45)
    
    plt.grid(True)
    plt.legend()
    
    # plt.show()
    
    plt.savefig("fig", dpi = 300, transparent = True, pad_inches = 0)